﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_TopUpManager : System.Web.UI.Page
{
    ServiceBlo _serviceBlo = new ServiceBlo();
    BillBlo _billBlo = new BillBlo();    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGridViewTopUP();
            lblStatus.Visible = false;
        }
    }
    private void LoadGridViewTopUP()
    {
        gdvTopUp.DataSource = _serviceBlo.SelectService("TopUp").ListServiceDetail;
        gdvTopUp.DataBind();
    }
    //protected void cboxCheckAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        CheckBox chkAll = (CheckBox)gdvTopUp.HeaderRow.Cells[0].FindControl("cboxCheckAll");
    //        foreach (GridViewRow row in gdvTopUp.Rows )
    //        {
    //            CheckBox chk = (CheckBox)row.Cells[0].FindControl("cBoxCheck");
    //            chk.Checked = chkAll.Checked;
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        ex.StackTrace.ToString();
    //    }
    //}
    //protected void gdvTopUp_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowIndex < 0) return;
    //    ((ImageButton)e.Row.Cells[6].FindControl("ibtnDelete")).Attributes.Add("onclick", "return confirm('Are you sure to delete this Top Up ?')");        
    //}
    //protected void gdvTopUp_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int id_SD = int.Parse(gdvTopUp.DataKeys[e.RowIndex].Value.ToString());
    //    lblStatus.Visible = true;
    //    if (DeleteTopUpServiceById(id_SD))
    //    {
    //        lblStatus.Text = "Delete Successfull";
    //    } 
    //    else
    //        lblStatus.Text = "Delete do not successfull! You must delete bill with constrain this TopUp";
    //}
    //private bool DeleteTopUpServiceById(int id_SD)
    //{
    //    if (_billBlo.SelectCountBillById_SD(id_SD) == 0)
    //    {
    //        _serviceBlo.DeleteServiceDetail(id_SD);
    //        //lblStatus.Text = "Delete Successfull";            
    //        LoadGridViewTopUP();
    //        return true;
    //    }
    //    else
    //    {
    //        //lblStatus.Text = "Delete do not successfull! You must delete bill with constrain this TopUp";
    //        //lblStatus.Visible = true;
    //        return false;
    //    }
    //}

    protected void gdvTopUp_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gdvTopUp.EditIndex = e.NewEditIndex;
        LoadGridViewTopUP();
    }
    protected void gdvTopUp_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdvTopUp.EditIndex = -1;
        LoadGridViewTopUP();
    }
    protected void gdvTopUp_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int id_SD = int.Parse(gdvTopUp.DataKeys[e.RowIndex].Value.ToString());
            int _duration = int.Parse(((TextBox)gdvTopUp.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            float _denomination = float.Parse(((TextBox)gdvTopUp.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
            int _discount = int.Parse(((TextBox)gdvTopUp.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
            float _total;
            _total = _denomination * _duration * (100 - _discount) / 100;
            ServiceDetail _serviceDetail = new ServiceDetail("", 0, _duration, "", _discount, _denomination, _total, id_SD);
            _serviceBlo.UpdateServiceDetail(_serviceDetail);
        }
        catch (Exception)
        {
            lblStatus.Visible = true;
            lblStatus.Text = " Your data is not valid! Please try again";
            gdvTopUp.EditIndex = e.RowIndex;
            LoadGridViewTopUP();
            return;
        }
        
        gdvTopUp.EditIndex = -1;
        LoadGridViewTopUP();
        lblStatus.Text = "Update successfull ";
        lblStatus.Visible = true;
    }
    protected void gdvTopUp_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id_SD = int.Parse(gdvTopUp.DataKeys[e.RowIndex].Value.ToString());
        lblStatus.Text = "get it";
        if (((LinkButton)gdvTopUp.Rows[e.RowIndex].Cells[5].FindControl("linkDeactive")).Visible == true)
        {
            _serviceBlo.UpdateServiceDetailStatus(id_SD, false);
            LoadGridViewTopUP();
        }
        else
        {
            _serviceBlo.UpdateServiceDetailStatus(id_SD, true);
            LoadGridViewTopUP();
        }        
    }
}